package httpunit;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.util.List;

public class JDListTools {
    public static void getItems() throws Exception {
        WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.waitForBackgroundJavaScript(10*1000);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        HtmlPage page = webClient.getPage("http://ma.inb123.com/d/30040");
        DomNodeList<DomNode> li = page.querySelectorAll("li");
        for (DomNode domElement : li) {
            String xml = domElement.asXml();
            if (xml.indexOf("品种名称") > -1) {
                Iterable<DomNode> domNodes = domElement.getChildren();
                for (DomNode node: domNodes) {
                    System.out.println(node.getTextContent());
                }
            }
//            System.out.println(domElement.asXml());
        }
        DomNodeList<DomNode> li2 = page.querySelectorAll("tr");
        for (DomNode domElement : li2) {
            String xml = domElement.asXml();
            if (xml.indexOf("品种名称") > -1) {
                Iterable<DomNode> domNodes = domElement.getChildren();
                for (DomNode node: domNodes) {
                    System.out.println(node.getTextContent());
                }
            }
//            System.out.println(domElement.asXml());
        }
        /*List<HtmlDivision> divs = (List) page.getByXPath("//div[@id='plist']//ul//li[@class='gl-item']//div[@class='gl-i-wrap j-sku-item']");
        for(HtmlDivision  div :divs) {
            DomNodeList<DomNode> childs = div.getChildNodes();
            String name = "";
            String price = "";
            String comments = "";
            for(DomNode dn : childs) {
                NamedNodeMap map = dn.getAttributes();
                Node node = map.getNamedItem("class");
                if(node != null) {
                    String value = node.getNodeValue();
                    if(value.contains("p-name")) {
                        name = dn.asText();
                    } else if(value.contains("p-price")) {
                        price = dn.asText();
                    } else if(value.contains("p-commit")) {
                        comments = dn.asText();
                    }
                }
            }
            System.out.println(name+"//"+price+"//"+comments);
        }*/
    }

    public static void main(String[] args) throws Exception {
        getItems();
    }
}
